package cn.edu.nju.askinginhand.activity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

public class PhotoTakenActivity extends Activity {
    /** Called when the activity is first created. */
	private static int SELECT_PICTURE;// 返回标志位 filed
	private File tempFile;
    ImageView imageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.phototaken);
        imageView = (ImageView) this.findViewById(R.id.image);

        Button edit = (Button) this.findViewById(R.id.edit);
        Button  ok=(Button) this.findViewById(R.id.ok);
        Button cancel = (Button) this.findViewById(R.id.cancel);
        
        takePhoto();
        edit.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                	edit();
                }
        });
        ok.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	
            	PhotoTakenActivity.this.setResult(RESULT_OK,PhotoTakenActivity.this.getIntent());
            	PhotoTakenActivity.this.finish();	
            }
    });
        cancel.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	tempFile = new File("/sdcard/camera.jpg");
            	tempFile.delete();
            	PhotoTakenActivity.this.finish();
            }
    });
        
    }
    
   private void takePhoto(){
	   
	   Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
       intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
                       .fromFile(new File(Environment
                                       .getExternalStorageDirectory(), "camera.jpg")));
       intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
       startActivityForResult(intent, 0);
   }
   private void edit(){
	   tempFile = new File("/sdcard/camera.jpg");//替换为你想要编辑的图片
   	Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(Uri.fromFile(tempFile), "image/*");//设置要裁剪的图片
		intent.putExtra("crop", "true");// crop=true 有这句才能出来最后的裁剪页面.

/*				intent.putExtra("aspectX", 1);// 这两项为裁剪框的比例.
		intent.putExtra("aspectY", 1);// x:y=1:1
		// 如果不设置aspectX和aspectY，则可以以任意比例缩放
*/				
/*				intent.putExtra("outputX", 96);
	    intent.putExtra("outputY", 96);//设置为96*96
*/				
		intent.putExtra("output", Uri.fromFile(tempFile));//保存到原文件
		intent.putExtra("outputFormat", "JPEG");// 返回格式
		startActivityForResult(Intent.createChooser(intent, "选择图片"),
				SELECT_PICTURE);
   }
   private void showImage(){
	   
	   
	   
   }
                           
    /*拍摄的照片存放在/mnt/sd/pictures/文件夹下*/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == 0 && resultCode == Activity.RESULT_OK) {
            	    String date=System.currentTimeMillis()+"";
            	    File directory=new File(Environment.getExternalStorageDirectory(),"pictures");
            	    if(!directory.exists())
            	        directory.mkdir();
            	    System.out.println(directory.getPath());
            	    File file1=new File(directory.getPath(),date+".jpg");
            	    
            	    try {
						if(file1.createNewFile()){
							File file2=new File(Environment.getExternalStorageDirectory(),"camera.jpg");
							FileInputStream fin=new FileInputStream(file2);
							FileOutputStream fout=new FileOutputStream(file1);
							byte[] buf=new byte[51200];
							while(true){
								   int ins=fin.read(buf);
								   if(ins==-1){
								      fin.close();
								      fout.flush();
								      fout.close();
				                      break;
								   }else
								      fout.write(buf,0,ins);
								  }
							PictureQuesActivity.uploadFile=file1.getAbsolutePath();
							this.imageView.setImageDrawable(Drawable.createFromPath(file1.getAbsolutePath()));
	                    // for(String s:bundle.keySet()){
	                    // Log.v("cam.demo", s);
	                    // }
	                   // this.imageView.setScaleType(ScaleType.CENTER_INSIDE);
	                    this.imageView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,500));
	                    Log.v("cam.demo", "====>" + data);
							
						}else{
							System.out.println("error");
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                    

            }
    }
    
}